<!--
Copyright (c) 2010 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
 -->
<html>
<head>
<link rel="stylesheet" href="../resources/js-test-style.css"/>
<script src="../resources/js-test-pre.js"></script>
<script src="resources/webgl-test.js"></script>
<script>
function runTest(gl, width, height)
{

    debug('Test whether the WebGL internal buffers have been initialized to 0.');
    var totalBytes = width * height * 4;
    var buf = new Uint8Array(totalBytes);
    gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf);
    if (gl.getError() != gl.NO_ERROR) {
        testFailed('GL error detected after readPixels().');
        return false;
    }
    for (var i = 0; i < totalBytes; ++i) {
        if (buf[i] != 0) {
            testFailed('WebGL internal buffers are dirty.');
            return false;
        }
    }
    testPassed('Buffers have been initialized to 0.');

    debug('Test whether user created buffers have been initialized to 0.');
    var fbo = gl.createFramebuffer();
    gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);
    var colorbuffer = gl.createRenderbuffer();
    gl.bindRenderbuffer(gl.RENDERBUFFER, colorbuffer);
    gl.renderbufferStorage(gl.RENDERBUFFER, gl.RGBA4, width, height);
    if (gl.getError() != gl.NO_ERROR) {
        testFailed('GL error detected after renderbufferStorage(internalformat = RGBA4).');
        return false;
    }
    gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, colorbuffer);
    if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE) {
        testFailed('Framebuffer incomplete.');
        return false;
    }
    gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf);
    if (gl.getError() != gl.NO_ERROR) {
        testFailed('GL error detected after readPixels().');
        return false;
    }
    for (var i = 0; i < totalBytes; ++i) {
        if (buf[i] != 0) {
            testFailed('User created buffers are dirty.');
            return false;
        }
    }

    testPassed('Buffers have been initialized to 0.');
    return true;
}
</script>
</head>
<body>
<canvas id="testbed" width="400px" height="400px"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
var successfullyParsed = false;

description('Verify renderbuffers are initialized to 0 before being read in WebGL');

var canvas = document.getElementById("testbed");
var gl = canvas.getContext("experimental-webgl");
if (!gl) {
    testFailed('canvas.getContext() failed');
} else {
    runTest(gl, canvas.width, canvas.height);

    // Testing that canvas resizing will clear the buffers with 0 instead of the current clear values.
    gl.clearColor(1, 0, 0, 1);
    canvas.width += 1;
    canvas.height += 1;
    runTest(gl, canvas.width, canvas.height);

    // Testing buffer clearing won't change the clear values.
    var clearColor = gl.getParameter(gl.COLOR_CLEAR_VALUE);
    shouldBe("clearColor", "[1, 0, 0, 1]");

    successfullyParsed = true;
}
</script>
<script src="../resources/js-test-post.js"></script>
</body>
</html>
